package com.example.utils;

import android.util.Log;

import com.example.bean.Result;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.google.gson.reflect.TypeToken;

import org.json.JSONArray;
import org.json.JSONObject;
import org.json.JSONTokener;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Dominic on 2017/2/23.
 */

public class JsonParser {
    public static String parseIatResult(String json) {
        StringBuffer ret = new StringBuffer() ;
        try {
            JSONTokener tokener = new JSONTokener(json) ;
            JSONObject joResult = new JSONObject(tokener) ;

            JSONArray words = joResult.getJSONArray("ws" );
            for (int i = 0; i < words.length(); i++) {
                // 转写结果词，默认使用第一个结果
                JSONArray items = words.getJSONObject(i).getJSONArray("cw" );
                JSONObject obj = items.getJSONObject(0 );
                ret.append(obj.getString("w" ));
//                  如果需要多候选结果，解析数组其他字段
//                 for(int j = 0; j < items.length(); j++)
//                 {
//                      JSONObject obj = items.getJSONObject(j);
//                      ret.append(obj.getString("w"));
//                 }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ret.toString();
    }

    public static String parseGrammarResult(String json) {
        StringBuffer ret = new StringBuffer() ;
        try {
            JSONTokener tokener = new JSONTokener(json) ;
            JSONObject joResult = new JSONObject(tokener) ;

            JSONArray words = joResult.getJSONArray("ws" );
            for (int i = 0; i < words.length(); i++) {
                JSONArray items = words.getJSONObject(i).getJSONArray("cw" );
                for (int j = 0; j < items.length() ; j++)
                {
                    JSONObject obj = items.getJSONObject(j);
                    if (obj.getString("w").contains( "nomatch"))
                    {
                        ret.append( "没有匹配结果.") ;
                        return ret.toString();
                    }
                    ret.append( "【结果】" + obj.getString("w" ));
                    ret.append("【置信度】 " + obj.getInt("sc" ));
                    ret.append("\n ");
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            ret.append(" 没有匹配结果 .");
        }
        return ret.toString();
    }

    public static String parseLocalGrammarResult(String json) {
        StringBuffer ret = new StringBuffer() ;
        try {
            JSONTokener tokener = new JSONTokener(json) ;
            JSONObject joResult = new JSONObject(tokener) ;

            JSONArray words = joResult.getJSONArray("ws" );
            for (int i = 0; i < words.length(); i++) {
                JSONArray items = words.getJSONObject(i).getJSONArray("cw" );
                for (int j = 0; j < items.length() ; j++)
                {
                    JSONObject obj = items.getJSONObject(j);
                    if (obj.getString("w").contains( "nomatch"))
                    {
                        ret.append( "没有匹配结果.") ;
                        return ret.toString();
                    }
                    ret.append( "【结果】" + obj.getString("w" ));
                    ret.append("\n ");
                }
            }
            ret.append("【置信度】 " + joResult.optInt("sc" ));

        } catch (Exception e) {
            e.printStackTrace();
            ret.append(" 没有匹配结果 .");
        }
        return ret.toString();
    }

    public static String parJson(String responseData) {
        Gson gson = new Gson();
        String strResult = "";
        Result result = gson.fromJson(responseData,new TypeToken<Result>(){}.getType());
        strResult = result.getResults().get(0).getValues().getText();
        Log.d("xxx","返回的结果为：" + strResult);
        return strResult;
    }

    /**
     * 解析成List<T>对象
     */
    public static <T> List<T> toList(String json, Class<T> clazz) {
        Type objectType = new TypeToken<ArrayList<JsonObject>>() {
        }.getType();

        Gson gson = new Gson();
        List<JsonObject> jsonObjectList = gson.fromJson(
                json, objectType);
        if (null == jsonObjectList) {
            return null;
        }
        List<T> list = new ArrayList<T>();
        for (JsonObject jsonObj : jsonObjectList) {
            list.add(gson.fromJson(jsonObj, clazz));
        }
        return list;
    }
}